package qiniu;

import com.common.model.Video;
import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import shujuku.PicCont;
import shujuku.VideoCont;

import java.io.File;

public class Qup extends Controller {

    public void upfile() throws QiniuException {
        UploadFile upfile = getFile("file", "mp4", 66666666);
        File f = upfile.getFile();
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传
        String accessKey = "GI0aWt10qLm_dUllw7k7xbyD1VCe8OOwzVje6abE";
        String secretKey = "jIg8E-bX45zUvR5u3VoYtNBLVkeltChz68wl7khN";
        String bucket = "video";
        //获取秘钥和touken
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        //上传 解析
        Response response = uploadManager.put(f, f.getName(), upToken);
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        //System.out.println(putRet.hash);
        String name = putRet.key;
        // renderJson("http://p2s56ya4m.bkt.clouddn.com/" + name);
        //获取文件后缀
        String filename = upfile.getFileName();
        String fileEnd = filename.substring(filename.lastIndexOf(".") + 1);
        String qiUrl = "http://p2s56ya4m.bkt.clouddn.com/" + name;
        /*后缀为图片的时候*/
        if ("jpg".equals(fileEnd) || "png".equals(fileEnd) || "bmp".equals(fileEnd) || "gif".equals(fileEnd)) {
            PicCont picCont = new PicCont();
            picCont.save(qiUrl);
            return;
        }
        VideoCont vt = new VideoCont();
        vt.save(qiUrl, filename, getPara("type"));
        renderJson("ok");
    }


}
